﻿/* 2011/12/28 */
using System;
using System.Xml;
using System.IO;
using System.Xml.Linq;

namespace Cosmos.Windows.Themes
{
    /// <summary>
    /// 文件夹信息。
    /// </summary>
    public struct FolderInformation
    {
        private string _foldPath;
        private bool _containSubfolder;
        private string _includePattern;
        private string _excludePattern;
        private GroupOption _groupOption;
        private string _groupPattern;
        private string _groupNamePattern;
        private bool _enabled;

        private static XmlReaderSettings _xmlReaderSettings;
        private static XmlWriterSettings _xmlWriterSettings;

        static FolderInformation()
        {
            _xmlReaderSettings = new XmlReaderSettings();
            _xmlReaderSettings.ConformanceLevel = ConformanceLevel.Fragment;
            _xmlReaderSettings.IgnoreWhitespace = true;
            _xmlReaderSettings.IgnoreComments = true;

            _xmlWriterSettings = new XmlWriterSettings();
            _xmlWriterSettings.ConformanceLevel = ConformanceLevel.Fragment;
            _xmlWriterSettings.Indent = false;
            _xmlWriterSettings.NewLineOnAttributes = false;
        }

        /// <summary>
        /// 初始化FolderInformation结构的新实例。
        /// </summary>
        /// <param name="folderPath">文件夹路径。</param>
        /// <param name="containSubfolder">是否包含子文件夹。</param>
        /// <param name="includePattern">包含文件匹配的模式。</param>
        /// <param name="excludePattern">排除文件匹配的模式。</param>
        /// <param name="groupOption">分组方式选项。</param>
        /// <param name="groupPattern">进行分组匹配的模式。</param>
        /// <param name="groupNamePattern">生成组名的模式。</param>
        /// <param name="enabled">是否启用该文件夹。</param>
        public FolderInformation(string folderPath, bool containSubfolder,
            string includePattern, string excludePattern,
            GroupOption groupOption, string groupPattern, string groupNamePattern, bool enabled)
        {
            _foldPath = folderPath;
            _containSubfolder = containSubfolder;
            _includePattern = includePattern;
            _excludePattern = excludePattern;
            _groupOption = groupOption;
            _groupPattern = groupPattern;
            _groupNamePattern = groupNamePattern;
            _enabled = enabled;
        }

        /// <summary>
        /// 初始化FolderInformation结构的新实例。
        /// </summary>
        /// <param name="escapeString">转义字符串。</param>
        public FolderInformation(string escapeString)
            : this()
        {
            LoadFromString(escapeString);
        }

        private void LoadFromString(string escapeString)
        {
            #region XmlReader
            /*
            StringReader stringReader = new StringReader(escapeString);
            XmlReader xmlReader = XmlReader.Create(stringReader, _xmlReaderSettings);

            xmlReader.ReadStartElement("Path");
            _foldPath = xmlReader.ReadContentAsString();
            xmlReader.ReadEndElement();
            xmlReader.ReadStartElement("ContainSubfolder");
            _containSubfolder = Boolean.Parse(xmlReader.ReadContentAsString());
            xmlReader.ReadEndElement();
            xmlReader.ReadStartElement("GroupOption");
            _groupOption = (GroupOption)Int32.Parse(xmlReader.ReadContentAsString());
            xmlReader.ReadEndElement();
            xmlReader.ReadStartElement("Enabled");
            _enabled = Boolean.Parse(xmlReader.ReadContentAsString());
            xmlReader.ReadEndElement();
            */
            #endregion

            StringReader reader = new StringReader(escapeString);
            XDocument document = XDocument.Load(reader);
            reader.Close();

            XElement rootElement = document.Element("FolderInformation");
            _foldPath = rootElement.Element("Path").Value;
            _containSubfolder = Boolean.Parse(rootElement.Element("ContainSubfolder").Value);
            _includePattern = rootElement.Element("IncludePattern").Value;
            _excludePattern = rootElement.Element("ExcludePattern").Value;
            _groupOption = (GroupOption)Int32.Parse(rootElement.Element("GroupOption").Value);
            _groupPattern = rootElement.Element("GroupPattern").Value;
            _groupNamePattern = rootElement.Element("GroupNamePattern").Value;
            _enabled = Boolean.Parse(rootElement.Element("Enabled").Value);
        }

        private string SaveToString()
        {
            #region XmlWriter
            /*
            StringWriter stringWriter = new StringWriter();
            XmlWriter xmlWriter = XmlWriter.Create(stringWriter, _xmlWriterSettings);
            
            xmlWriter.WriteStartElement("Path");
            xmlWriter.WriteString(_foldPath);
            xmlWriter.WriteEndElement();
            xmlWriter.WriteStartElement("ContainSubfolder");
            xmlWriter.WriteString(_containSubfolder.ToString());
            xmlWriter.WriteEndElement();
            xmlWriter.WriteStartElement("GroupOption");
            xmlWriter.WriteString(((int)_groupOption).ToString());
            xmlWriter.WriteEndElement();
            xmlWriter.WriteStartElement("Enabled");
            xmlWriter.WriteString(_enabled.ToString());
            xmlWriter.WriteEndElement();

            xmlWriter.Flush();
            xmlWriter.Close();
            stringWriter.Flush();
            string result = stringWriter.ToString();
            stringWriter.Close();
            return result;
             */
            #endregion

            XDocument document = new XDocument();
            XElement rootElement = new XElement("FolderInformation");
            document.Add(rootElement);
            rootElement.Add(new XElement("Path",_foldPath));
            rootElement.Add(new XElement("ContainSubfolder", _containSubfolder.ToString()));
            rootElement.Add(new XElement("IncludePattern", _includePattern));
            rootElement.Add(new XElement("ExcludePattern", _excludePattern));
            rootElement.Add(new XElement("GroupOption", ((int)_groupOption).ToString()));
            rootElement.Add(new XElement("GroupPattern", _groupPattern));
            rootElement.Add(new XElement("GroupNamePattern", _groupNamePattern));
            rootElement.Add(new XElement("Enabled", _enabled.ToString()));

            StringWriter writer = new StringWriter();
            document.Save(writer);
            writer.Flush();
            string escapeString = writer.ToString();
            writer.Close();
            return escapeString;
        }

        /// <summary>
        /// 获取或设置文件夹路径。
        /// </summary>
        public string FolderPath
        {
            get { return _foldPath; }
            set { _foldPath = value; }
        }

        /// <summary>
        /// 获取或设置是否包含子文件夹。
        /// </summary>
        public bool ContainSubfolder
        {
            get { return _containSubfolder; }
            set { _containSubfolder = value; }
        }

        /// <summary>
        /// 获取或设置包含文件匹配的模式。
        /// </summary>
        public string IncludePattern
        {
            get { return _includePattern; }
            set { _includePattern = value; }
        }

        /// <summary>
        /// 获取或设置排除文件匹配的模式。
        /// </summary>
        public string ExcludePattern
        {
            get { return _excludePattern; }
            set { _excludePattern = value; }
        }

        /// <summary>
        /// 获取或设置分组方式选项。
        /// </summary>
        public GroupOption GroupOption
        {
            get { return _groupOption; }
            set { _groupOption = value; }
        }

        /// <summary>
        /// 获取或设置解析路径进行的模式。
        /// </summary>
        public string GroupPattern
        {
            get { return _groupPattern; }
            set { _groupPattern = value; }
        }

        /// <summary>
        /// 获取或设置生成组名的模式。
        /// </summary>
        public string GroupNamePattern
        {
            get { return _groupNamePattern; }
            set { _groupNamePattern = value; }
        }

        /// <summary>
        /// 获取或设置是否启用该文件夹。
        /// </summary>
        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        /// <summary>
        /// 获取当前状态的转义字符串，或通过设置转义字符串改变状态。
        /// </summary>
        public string EscapeString
        {
            get { return SaveToString(); }
            set { LoadFromString(value); }
        }
    }
}
